package main.ServerTest;

/**
 * 作者: ALIM-MASTIK
 * 创建实现: 2023/7/15 9:40
 * 项目名称：chat
 * 软件: IntelliJ IDEA
 */
import main.Daoimpl.ChatRoomDAOImpl;
import main.JdbcUtils;
import main.Service.ChatRoomService;
import main.ServiceImpli.ChatRoomServiceImpl;
import main.dao.ChatRoomDAO;
import main.pojo.ChatRoom;
import org.apache.commons.dbutils.DbUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class ChatRoomServiceImplTest {
    private static Connection connection=JdbcUtils.getConnection();
    private ChatRoomService chatRoomService;

    @BeforeAll
    public static void setup() throws SQLException {
        connection = JdbcUtils.getConnection();
        // 创建 chatrooms 表并插入测试数据
        try {
            TestUtil.createChatRoomsTable(connection);
            TestUtil.insertTestDataIntoChatRooms(connection);
        } catch (SQLException e) {

        }
    }

    @BeforeEach
    public void init() {
        ChatRoomDAO chatRoomDAO = new ChatRoomDAOImpl(connection);
        chatRoomService = new ChatRoomServiceImpl(chatRoomDAO);
    }

    @Test
    public void testGetChatRoomByID() throws SQLException {
        int chatRoomID = 4;
        ChatRoom chatRoom = chatRoomService.getChatRoomByID(chatRoomID);
        assertNotNull(chatRoom);
        System.out.println(chatRoom.getChatRoomName());
    }

    @Test
    public void testGetAllChatRooms()  {
        List<ChatRoom> chatRooms = chatRoomService.getAllChatRooms();
        for (ChatRoom chatRoom:chatRooms){
            System.out.println(chatRoom.toString());
        }
        assertNotNull(chatRooms);
//        assertFalse(chatRooms.isEmpty());
//        assertEquals(2, chatRooms.size());
    }

    @Test
    public void testCreateChatRoom()   {
        ChatRoom chatRoom = new ChatRoom();
        chatRoom.setChatRoomID(43);
        chatRoom.setChatRoomName("Chat Room 114");
        chatRoomService.createChatRoom(chatRoom);
        List<ChatRoom> chatRooms = chatRoomService.getAllChatRooms();
        assertNotNull(chatRooms);
//        assertEquals(4, chatRooms.size());
    }

    @Test
    public void testUpdateChatRoom() throws SQLException {
        int chatRoomID = 2;
        String newChatRoomName = "Updated Chat Room";
        ChatRoom chatRoom = chatRoomService.getChatRoomByID(chatRoomID);
        assertNotNull(chatRoom);
        chatRoom.setChatRoomName(newChatRoomName);
        chatRoomService.updateChatRoom(chatRoom);
        ChatRoom updatedChatRoom = chatRoomService.getChatRoomByID(chatRoomID);
        assertNotNull(updatedChatRoom);
        assertEquals(newChatRoomName, updatedChatRoom.getChatRoomName());
    }

    @Test
    public void testDeleteChatRoom() throws SQLException {
        int chatRoomID = 1;
        chatRoomService.deleteChatRoom(chatRoomID);
        List<ChatRoom> chatRooms = chatRoomService.getAllChatRooms();
        assertNotNull(chatRooms);
        assertEquals(14, chatRooms.size());
    }

    @AfterAll
    public static void cleanup() throws SQLException {
        DbUtils.closeQuietly(connection);
    }
}
